package com.example.phoneshop.controller.admin;

import com.example.phoneshop.config.Config;
import com.example.phoneshop.entity.User;
import com.example.phoneshop.service.UserService;
import com.example.phoneshop.tool.ResultJson;
import com.example.phoneshop.tool.Tool;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "admin/user")
public class UserAdmin {
    private UserService userService;

    public UserAdmin(UserService userService) {
        this.userService = userService;
    }

    private String[] menu;

    /**
     * 用户信息查看，以及删除操作等的表格页面
     *
     * @param request
     * @param session
     * @param response
     * @return
     */
    @RequestMapping(value = {"index", "/", ""}, method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request, HttpSession session, HttpServletResponse response) {
        // 判断是否登录
        if (AdminOrderController.isAdminLogin(session, response)) {
            return null;
        }
        ModelAndView model = new ModelAndView("admin/user/index");
        // 获取导航栏的active数组
        menu = Config.getMenu();
        model.addObject("user", (User) session.getAttribute("user"));
        model.addObject("title", "用户信息");
        model.addObject("classurl", request.getContextPath() + "/admin/index");
        model.addObject("classname", "用户管理");
        menu[6] = "open active";
        menu[7] = "active";
        List<Map> mapList = null;
        try {
            mapList = userService.findUserAll();
            mapList = Tool.getDateObject(mapList, "registertime");
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addObject("data", mapList);
        model.addObject("menu", menu);
        return model;
    }

    /**
     * 用户删除方法
     * @param request
     * @param response
     * @param session
     * @param id
     * @return
     */

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public @ResponseBody
    ResultJson delete(HttpServletRequest request, HttpServletResponse response, HttpSession session, @RequestParam("id") int id) {
        ResultJson resultJson = new ResultJson();
        if (AdminOrderController.isAdminLogin(session, response)) {
            return null;
        }
        int result = userService.delete(id);
        if (result == 1) {
            resultJson.setText("ok");
        } else {
            resultJson.setText("删除失败，请稍后再试！");
        }
        return resultJson;
    }

}






























